<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Resin CGIServlet</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="jee tutorial, java, Resin, CGIServlet">
<meta name="description" content="In this part of the Java EE 5 tutorials, we present a Resin CGIServlet.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content">


<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Resin CGIServlet</h1>

<p>
In this tutorial, we will show how to use Resin's CGIServlet. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 

<p>
The <b>CGI (Common Gateway Interface)</b>, is a protocol for calling external 
software via a web server to deliver dynamic content.Resin enables this protocol 
through it's <b>CGIServlet</b>.
</p>

<h2>CGIServlet</h2>

<p>
Implements CGI calls. The url is assumed to refer to an executable file. The 
operating system is instructed to execute the program or script. The usual CGI 
environment variables are set, and the current directory is the value of $RESIN_HOME
</p>

<p>
In our code example, we will demonstrate how to execute a shell script, Python 
script, Ruby script and a Perl script. 
</p>

<div class="codehead">web.xml</div>
<pre class="code">
&lt;?xml version = '1.0' encoding = 'UTF-8'?&gt;
&lt;web-app&gt;
  &lt;description&gt;CGI scripts&lt;/description&gt;
  &lt;servlet&gt;
    &lt;servlet-name&gt;cgi&lt;/servlet-name&gt;
    &lt;servlet-class&gt;com.caucho.servlets.CGIServlet&lt;/servlet-class&gt;
  &lt;/servlet&gt;
  
  &lt;servlet-mapping&gt;
    &lt;servlet-name&gt;cgi&lt;/servlet-name&gt;
    &lt;url-pattern&gt;/cgi-bin/*.sh&lt;/url-pattern&gt;
    &lt;url-pattern&gt;/cgi-bin/*.py&lt;/url-pattern&gt;
    &lt;url-pattern&gt;/cgi-bin/*.rb&lt;/url-pattern&gt;
    &lt;url-pattern&gt;/cgi-bin/*.pl&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;welcome-file-list&gt;
    &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
&lt;/web-app&gt;
</pre>

<p>
This is the <i>web.xml</i> file. Here we map all urls 
with .sh, .py, .rb and .pl extensions to the CGIServlet. Traditionally, 
the cgi scripts are placed in a directory called <i>cgi-bin</i>. 
</p>

<div class="codehead">index.jsp</div>
<pre class="code">
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;CGI scripts&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
   &lt;pre&gt;
   Execute:

   &lt;a href="http://localhost:8080/webapp/cgi-bin/script.sh"&gt;shell script&lt;/a&gt;
   &lt;a href="http://localhost:8080/webapp/cgi-bin/script.py"&gt;Python script&lt;/a&gt;
   &lt;a href="http://localhost:8080/webapp/cgi-bin/script.rb"&gt;Ruby script&lt;/a&gt;
   &lt;a href="http://localhost:8080/webapp/cgi-bin/script.pl"&gt;Perl script&lt;/a&gt;
   &lt;/pre&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>


<p>
This is the index.jsp page. Here we have four links to four different cgi scripts. 
</p>


<pre class="code">
$ ls *
index.html

cgi-bin:
script.pl  script.py  script.rb  script.sh

META-INF:
resin-war.digest

WEB-INF:
classes  tmp  web.xml
</pre>

<p>
This is how the directory structure looks like. Note the cgi-bin directory. 
</p>


<h2>Scripts</h2>

<p>
Next we will show all four CGI scripts. They all print environment variables. 
</p>

<div class="codehead">script.sh</div>
<pre class="code">
#!/bin/sh

echo "Content-Type: text/html"
echo

echo "&lt;pre&gt;"
env
echo "&lt;/pre&gt;"
</pre>

<p>
This is the shell CGI script. 
</p>

<div class="codehead">script.py</div>
<pre class="code">
#!/usr/bin/python

import sys
import os
from cgi import escape

print "Content-type: text/html"
print
print "&lt;pre&gt;"
for k in sorted(os.environ):
    print "&lt;b&gt;%s: &lt;/b&gt;%s" %(escape(k), escape(os.environ[k]))
print "&lt;/pre&gt;"
</pre>

<p>
This is the Python CGI script. 
</p>

<div class="codehead">script.rb</div>
<pre class="code">
#!/usr/bin/ruby

puts "Content-Type: text/html"
puts

puts "&lt;pre&gt;"
ENV.to_hash.each do |key, value|
    puts("&lt;b&gt;#{key}&lt;/b&gt;: #{value}")
end
puts "&lt;/pre&gt;"

</pre>

<p>
This is the Ruby CGI script. 
</p>


<div class="codehead">script.pl</div>
<pre class="code">
#!/usr/bin/perl

print "Content-type: text/html\n\n";

print "&lt;pre&gt;";
foreach $key (sort keys(%ENV)) {
   print "&lt;b&gt;$key&lt;/b&gt;: $ENV{$key}";
   print "&lt;br&gt;";
} 
print "&lt;/pre&gt;";
</pre>

<p>
Finally, this is the Perl CGI script. 
</p>

<img src="/img/java/jee/cgiservlet.png" alt="CGI scripts">
<div class="figure">Figure: CGI script</div>



<p>
In this chapter, we have used the Resin's CGIServlet to 
launch Python, PERL and Bash scripts. 
</p>

<div class="center"> 
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* horizontal */
google_ad_slot = "1734478269";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script> 
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> 
</script> 
</div> 
<br> 


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified October 15, 2008  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>


